﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{ 
    [Category("VIBlend.WPF.Examples")]
    public partial class GridValidation : Window, IWindow
    {
        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
        }
  
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        public GridValidation()
        {
            InitializeComponent();

            PrepareGridData();

            dataGrid.ItemsSource = listSaleRecord;
            
            // Hide the Rows Hierarchy.
            dataGrid.RowsHierarchy.Visible = false;

            // Subscribe to the CellValidating event to validate the user input.
            dataGrid.CellValidating += new VIBlend.WPF.Controls.DataGrid.CellCancelEventHandler(dataGrid_CellValidating);
            this.LayoutRoot.Children.Add(provider);
        }

        ErrorProvider provider = new ErrorProvider();
        
        void dataGrid_CellValidating(object sender, CellCancelEventArgs args)
        {
            // If the entered value is not valid, display an error message that will notify the user.
            if (args.Cell.ColumnItem.Text.Equals("UnitPrice"))
            {
                double value = (double)(args.Cell.EditorElement as SpinEditor).Value;
                if (value < 1 || value > 100)
                {
                    provider.BlinkStyle = ErrorProviderBlinkStyle.BlinkIfDifferentError;
                    provider.IconAlignment = ErrorProviderIconAlignment.MiddleLeft;
                    provider.SetError(args.Cell.EditorElement, "The entered value should be between 1 and 100");
                  
                    args.Cancel = true;
                }
            }
            else if (args.Cell.ColumnItem.Text.Equals("Quantity"))
            {
                double value = (double)(args.Cell.EditorElement as SpinEditor).Value;
                if (value < 1 || value > 10)
                {
                    provider.BlinkStyle = ErrorProviderBlinkStyle.BlinkIfDifferentError;
                    provider.IconAlignment = ErrorProviderIconAlignment.MiddleRight;
                    provider.SetError(args.Cell.EditorElement, "The entered value should be between 1 and 10");
                    args.Cancel = true;
                }
            }
        }

        void dataGrid_CellEditorActivated(object sender, CellEventArgs args)
        {
            Control control = (Control)args.Cell.EditorElement;
            if (control == null)
                return;

            control.KeyDown += new KeyEventHandler(control_KeyDown);
        }

        void control_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                dataGrid.CloseEditor(true);
            else
                if (e.Key == Key.Enter)
                    dataGrid.CloseEditor(false);
        }


        #region Sample Data

      
        public List<EmployeeSaleRecordValidationDemo> listSaleRecord = new List<EmployeeSaleRecordValidationDemo>();

        private void PrepareGridData()
        {
            const int rowsToLoad = 1000;
            listSaleRecord.Clear();

            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
                int productId = rand.Next(0, productNames.Length / 2 - 1);
                int quantity = rand.Next(1, 5);
                listSaleRecord.Add(new EmployeeSaleRecordValidationDemo(
                    productId,
                    quantity)
                    );
            }
        }

        static string[,] productNames = new string[,] 
                {
                    {"Black Tea", "2"},
                    {"Green Tea", "1"},
                    {"Caffè Espresso", "1"},
                    {"Doubleshot Espresso", "3"},
                    {"Caffè Latte", "2"},
                    {"White Chocolate Mocha", "2"},
                    {"Caramel Latte", "3"},
                    {"Caffè Americano", "2"},
                    {"Cappuccino", "2"},
                    {"Espresso Truffle", "3"},
                    {"Espresso con Panna", "2"},
                    {"Peppermint Mocha Twist", "2"}
                };

        #endregion

        protected override void OnClosing(CancelEventArgs e)
        {
            this.listSaleRecord.Clear();
            this.listSaleRecord = null;
            base.OnClosing(e);
            this.dataGrid.CellEditorActivated -= new VIBlend.WPF.Controls.DataGrid.CellEventHandler(dataGrid_CellEditorActivated);
            this.dataGrid.CellValidating -= new VIBlend.WPF.Controls.DataGrid.CellCancelEventHandler(dataGrid_CellValidating);
       
            this.dataGrid.Dispose();
            this.dataGrid = null;
        }
    
    }

    public class EmployeeSaleRecordValidationDemo
    {
        static string[,] productNames = new string[,] 
                {
                    {"Black Tea", "2"},
                    {"Green Tea", "2"},
                    {"Caffè Espresso", "1"},
                    {"Doubleshot Espresso", "2"},
                    {"Caffè Latte", "3"},
                    {"White Chocolate Mocha", "2"},
                    {"Caramel Latte", "3"},
                    {"Caffè Americano", "2"},
                    {"Cappuccino", "2"},
                    {"Espresso Truffle", "3"},
                    {"Espresso con Panna", "1"},
                    {"Peppermint Mocha Twist", "2"}
                };

        public EmployeeSaleRecordValidationDemo(int ProductId, int Quantity)
        {
            this._productid = ProductId;
            this._quantity = Quantity;
        }

        #region Private Members
        private int _productid;
        private int _quantity;
        #endregion

        #region Properties

        public int Product
        {
            get { return _productid; }
            set { _productid = value; }
        }

        public int Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }

        public double UnitPrice
        {
            get
            {
                double unitPrice = double.Parse(productNames[_productid, 1]);
                return unitPrice;
            }
            set
            {
                productNames[_productid, 1] = value.ToString();
            }

        }
        #endregion
    }

}
